//
//  ShouyeViewController.m
//  中皮网
//
//  Created by ios程序员 on 15/10/29.
//  Copyright (c) 2015年 ios程序员. All rights reserved.
//

#import "ShouyeViewController.h"

@interface ShouyeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *toucollectionView;
@property (nonatomic ,strong) UISearchBar *searchBar;
@property (nonatomic ,strong) HeaderViews *headerView;
@property (nonatomic ,strong) KanjiaView *kanjiaView;
@property (nonatomic ,strong) TuiJianView *tuijianView;
@property (nonatomic ,strong) jingPinView *jinPinView;
@property (nonatomic ,strong) LikeView *likeView;
@property (nonatomic ,strong) NSMutableArray *arr;
@property (nonatomic ,strong) NSMutableDictionary *dic;
@property (nonatomic ,strong) ShouyeCollecViewController *coll;
@property (nonatomic ,strong) Collection *wheel;
@property (nonatomic ,strong) NSMutableArray *wheelStrArr;
@property (nonatomic ,strong) UIButton *messageButton;
@end

@implementation ShouyeViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.saoButton.alpha = 1;
    self.searchBar.alpha = 1;
    self.tabBarController.tabBar.alpha = 1;
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.saoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saoButton.frame = CGRectMake(15, 0, 40, 40);
    [self.saoButton setImage:[UIImage imageNamed:@"sao.png"] forState:UIControlStateNormal];
//    [self.saoButton setTitle:@"扫一扫" forState:UIControlStateNormal];
    self.saoButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [self.saoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:self.saoButton];
    
    [self.saoButton addTarget:self action:@selector(saoButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.messageButton.frame = CGRectMake(320*HEIGH, 8, 50*HEIGH, 25);
    [self.messageButton setImage:[UIImage imageNamed:@"shop19"] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:self.messageButton];
    [self.messageButton addTarget:self action:@selector(messageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    

    
#pragma mark - 回首键盘
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyBoardjide:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];

    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake( 60, 10, W * 250 / 375, H * 25 / 667)];
    self.searchBar.layer.borderWidth = 0;
//    self.searchBar.keyboardType = 1;
    self.searchBar.placeholder = @"商品搜索OR品牌";
    [self.searchBar setTranslucent:YES];
    
    self.searchBar.searchBarStyle = 2;
    self.searchBar.searchResultsButtonSelected = YES;
    
    [self.navigationController.navigationBar addSubview:self.searchBar];
    self.searchBar.delegate = self;

    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    /* 更改item 大小 默认（50 * 50） */
    flowLayout.itemSize = CGSizeMake(w / 2-20, w / 2 -20);
    /* 设置item 上下之间的距离（最小值）*/
    flowLayout.minimumLineSpacing = 5;
    /* 设置item 左右之间的距离（最小值） */
    flowLayout.minimumInteritemSpacing = 5;
    /* 设置距四边地距离 */
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    /* 创建对象 */
    self.toucollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    /* 背景颜色默认是黑色 */
    /* 两个协议 */
    self.toucollectionView.delegate = self;
    self.toucollectionView.dataSource =self;
    self.toucollectionView.backgroundColor = [UIColor whiteColor];
//    self.toucollectionView.backgroundColor = [UIColor colorWithRed:0.948 green:1.000 blue:0.855 alpha:1.000];
    [self.toucollectionView registerClass:[ShouYecollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    [self.toucollectionView registerClass:[HeaderViews class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    [self.toucollectionView registerClass:[KanjiaView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kanJiaReuse"];
    [self.toucollectionView registerClass:[TuiJianView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"tuiJianReuse"];
    [self.toucollectionView registerClass:[jingPinView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"jingPinReuse"];
    [self.toucollectionView registerClass:[LikeView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"likeReuse"];
    
    [self.view addSubview:self.toucollectionView];
    [self headerViewss];
    [self kanjiaViews];
    [self tuijianViews];
    [self jingpinViews];
    [self likeViews];
    
    [self dateHandle];

}
/* 判断navi 状态  */
#if 0
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.toucollectionView) {
        if (scrollView.contentOffset.y >= -20) {
            self.navigationController.navigationBar.frame = CGRectMake(0, -scrollView.contentOffset.y, self.view.frame.size.width, 64);
        }
    }
}
# endif
#pragma mark - 回首键盘方法
//*************************************************************************
- (void)keyBoardjide:(UITapGestureRecognizer *)tap
{
    [self.searchBar resignFirstResponder];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    
    [self.session stopRunning];
    
    [self.preview removeFromSuperlayer];
    
    
    NSString *val = nil;
    if (metadataObjects.count > 0)
    {
        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
        
        val = obj.stringValue;
        
        NSLog(@"val = %@",val);
    }
}


//轮播图View
- (void)headerViewss
{
    self.wheelStrArr = [NSMutableArray array];//这个数组里面装model对象
    //    for (NSMutableDictionary *dic in arr) {
    //
    //        HomePage *home = [[HomePage alloc]init];
    //
    //        [home setValuesForKeysWithDictionary:dic];
    //
    //        [self.wheelStrArr addObject:home];
    //
    //
    //    }
    self.wheel = [[Collection alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 195*HEIGH)];
    NSMutableArray *array = [NSMutableArray array];
    [self.toucollectionView addSubview:self.wheel];
    self.wheel.imageArr = array;//array 是model里面图片属性的数组（字符串网址形式）
    //    for (HomePage *home in self.wheelStrArr) {
    //
    //        [array addObject:home.photo];
    //
    //    }
    
    // 为轮播图添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wheelTapAction:)];
    
    [self.wheel.collection addGestureRecognizer:tap];
    
}

//接收消息方法
- (void)messageButtonClick:(UIButton *)button
{
    
}


// 轮播图的点击方法
- (void)wheelTapAction:(UITapGestureRecognizer *)tap
{
    NSInteger i = [tap locationInView:self.wheel.collection].x/self.view.bounds.size.width;
    
    if (i == 0) {
        
        i = self.wheelStrArr.count - 1;
    }
    
    if (i == self.wheelStrArr.count + 1) {
        
        i = 1;
    }
    //push到新界面
}


//网络数据
- (void)dateHandle
{
    NSString *shouYeurl = @"";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:shouYeurl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"json:::::%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

//砍价View
- (void)kanjiaViews
{
    self.kanjiaView = [[KanjiaView alloc] initWithFrame:CGRectMake(0, 198*HEIGH,w, 120*HEIGH)];
    UITapGestureRecognizer *kanjiaTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(kanjiaViewClick)];
    self.kanjiaView.imageView.userInteractionEnabled = YES;
    [self.kanjiaView.imageView addGestureRecognizer:kanjiaTap];
    [self.toucollectionView addSubview:self.kanjiaView];
    
    //抽奖
    UITapGestureRecognizer  *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(huoDong)];
    self.kanjiaView.imageView1.userInteractionEnabled = YES;
    [self.kanjiaView.imageView1 addGestureRecognizer:tap];
    
    //抢购
    UITapGestureRecognizer *image2Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qianggou)];
    self.kanjiaView.imageView2.userInteractionEnabled = YES;
    [self.kanjiaView.imageView2 addGestureRecognizer:image2Tap];
    
    UITapGestureRecognizer *image3Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qianggou)];
    self.kanjiaView.imageView2.userInteractionEnabled = YES;
    [self.kanjiaView.imageView2 addGestureRecognizer:image3Tap];

    
}
/* 抽奖跳转 */
- (void)huoDong
{
    CJViewController *cj = [[CJViewController alloc] init];
    [self.navigationController pushViewController:cj animated:YES];
    self.saoButton.alpha = 0;
}
/* 抢购详情 */
- (void)qianggou
{
    SYdaTeViewController *sydaTe = [[SYdaTeViewController alloc] init];
    [self.navigationController pushViewController:sydaTe animated:YES];
    self.saoButton.alpha = 0;
}

//最新推荐View
- (void)tuijianViews
{
    self.tuijianView = [[TuiJianView alloc] initWithFrame:CGRectMake(0, 330*HEIGH, self.view.frame.size.width, 160*HEIGH)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuijianXiangqing2)];
    self.tuijianView.imageView2.userInteractionEnabled = YES;
    [self.tuijianView.imageView2 addGestureRecognizer:tap2];
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuijianXiangqing3)];
    self.tuijianView.imageView3.userInteractionEnabled = YES;
    [self.tuijianView.imageView3 addGestureRecognizer:tap3];
    
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tuijianXiangqing4)];
    self.tuijianView.imageView4.userInteractionEnabled = YES;
    [self.tuijianView.imageView4 addGestureRecognizer:tap4];


    
    [self.toucollectionView addSubview:self.tuijianView];
}
//精品 天天 全民 View
- (void)jingpinViews
{
    self.jinPinView = [[jingPinView alloc] initWithFrame:CGRectMake(0, 515*HEIGH, w, 120*HEIGH)];
    [self.toucollectionView addSubview:self.jinPinView];
}

- (void)likeViews
{
    self.likeView = [[LikeView alloc] initWithFrame:CGRectMake(0, 670*HEIGH, w, 120*HEIGH)];
    [self.toucollectionView addSubview:self.likeView];
}

#pragma collectionView 两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShouYecollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:240/255.0 green:241/255.0 blue:242/255.0 alpha:1];
    cell.shouyeImage.backgroundColor = [UIColor greenColor];
//    cell.jianJieLabel.backgroundColor = [UIColor purpleColor];
//    cell.priceLabel.backgroundColor = [UIColor blueColor];
    cell.newpinxLabel.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SYdaTeViewController *dates = [[SYdaTeViewController alloc] init];
    [self.navigationController pushViewController:dates animated:NO];
    self.saoButton.alpha = 0;
    self.searchBar.alpha = 0;
//    self.tabBarController.tabBar.hidden = YES;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 780*HEIGH);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(w, 50*HEIGH);
}

//最新推荐 image2 tap手势详情跳转
- (void)tuijianXiangqing2
{
    SYdaTeViewController *sydaTe = [[SYdaTeViewController alloc] init];
    [self.navigationController pushViewController:sydaTe animated:YES];
    self.saoButton.alpha = 0;
    
}
//最新推荐 image3 tap手势详情跳转
- (void)tuijianXiangqing3
{
    SYdaTeViewController *sydaTe = [[SYdaTeViewController alloc] init];
    [self.navigationController pushViewController:sydaTe animated:YES];
    self.saoButton.alpha = 0;

    
}
//最新推荐 image4 tap手势详情跳转
- (void)tuijianXiangqing4
{
    SYdaTeViewController *sydaTe = [[SYdaTeViewController alloc] init];
    [self.navigationController pushViewController:sydaTe animated:YES];
    self.saoButton.alpha = 0;

}

//扫描
- (void)saoButtonClick
{
    lhScanQCodeViewController * sqVC = [[lhScanQCodeViewController alloc]init];
    UINavigationController * nVC = [[UINavigationController alloc]initWithRootViewController:sqVC];
    [self presentViewController:nVC animated:YES completion:^{
        
    }];
}

//搜索协议方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    PiShangViewController *piShang = [[PiShangViewController alloc] init];
    [self.navigationController pushViewController:piShang animated:NO];
    self.searchBar.alpha = 0;
}

//砍价跳转登陆页面
- (void)kanjiaViewClick
{
    DengLuViewController *denglu = [[DengLuViewController alloc] init];
    [self.navigationController pushViewController:denglu animated:NO];
    self.saoButton.alpha = 0;
    self.tabBarController.tabBar.alpha = 0;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
